package top.yuechenc.manage.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.yuechenc.manage.common.token.ShopResult;
import top.yuechenc.manage.common.token.UserLoginToken;
import top.yuechenc.manage.common.utils.SpStringUtils;
import top.yuechenc.manage.entity.SpAttribute;
import top.yuechenc.manage.service.AttributeService;

import java.util.List;
import java.util.UUID;

/**
 * 商品属性管理接口
 *
 * @author Zhiwei Wang
 * @date 2020/3/12 15:58
 */
@Api(description = "商品属性管理接口")
@RestController
@RequestMapping(value = "/api/attr")
public class AttributeController {
    private static final Logger log = LoggerFactory.getLogger(AttributeController.class);

    @Autowired
    private AttributeService attributeService;

    @ApiOperation(value = "获取商品属性列表")
    @UserLoginToken
    @PostMapping(value = "/getAttrList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", dataType = "int", paramType = "query", defaultValue = "10")
    })
    private ShopResult getAttrList(@RequestBody @ApiParam(name = "商品属性对象", value = "传入json对象") SpAttribute spAttribute, int pageNum, int pageSize) {
        ShopResult shopResult = null;
        try {
            PageInfo pageInfo = attributeService.findListByParam(spAttribute, pageNum, pageSize);
            shopResult = new ShopResult(ShopResult.Metaa.SELECT, pageInfo);
            log.info("分页获取商品属性列表成功，pageInfo={}",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            log.debug(e.toString());
            shopResult = new ShopResult(ShopResult.Metaa.CREATED_FAIL, e.toString());
        }
        return shopResult;
    }

    @ApiOperation(value = "根据编号获取属性信息")
    @UserLoginToken
    @PostMapping("/findById")
    private ShopResult findById(@RequestBody @ApiParam(name = "商品属性对象", value = "传入json对象") SpAttribute spAttribute) {
        ShopResult shopResult = null;
        if (!SpStringUtils.isNotEmpty(spAttribute.getAttrId())) {
            shopResult = new ShopResult(ShopResult.Metaa.SELECT_FAIL, "请输入属性编号！");
        } else if (!SpStringUtils.isNotEmpty(spAttribute.getCatId())) {
            shopResult = new ShopResult(ShopResult.Metaa.SELECT_FAIL, "请输入分类编号！");
        } else if (!SpStringUtils.isNotEmpty(spAttribute.getAttrSel())) {
            shopResult = new ShopResult(ShopResult.Metaa.SELECT_FAIL, "请输入分类编号！");
        } else {
            try {
                List<SpAttribute> list = attributeService.getByParam(spAttribute);
                if (list.size() > 0) {
                    log.info("根据编号获取属性信息，spAttribute={}",list.get(0));
                    return new ShopResult(ShopResult.Metaa.SELECT, list.get(0));
                } else {
                    shopResult = new ShopResult(ShopResult.Metaa.SELECT_FAIL, "不存在满足条件的属性！");
                }
            } catch (Exception e) {
                log.debug(e.toString());
                shopResult = new ShopResult(ShopResult.Metaa.SELECT_FAIL, e);
            }
        }
        return shopResult;
    }

    @ApiOperation(value = "添加商品属性")
    @UserLoginToken
    @PostMapping("/addAttr")
    private ShopResult addAttr(@RequestBody @ApiParam(name = "商品属性对象", value = "传入json对象", required = true) SpAttribute spAttribute) {
        ShopResult shopResult = null;
        if (!SpStringUtils.isNotEmpty(spAttribute.getCatId())) {
            shopResult = new ShopResult(ShopResult.Metaa.CREATED_FAIL, "请输入分类编号！");
        } else if (!SpStringUtils.isNotEmpty(spAttribute.getAttrSel())) {
            shopResult = new ShopResult(ShopResult.Metaa.CREATED_FAIL, "请输入属性类型！");
        } else if (!SpStringUtils.isNotEmpty(spAttribute.getAttrName())) {
            shopResult = new ShopResult(ShopResult.Metaa.CREATED_FAIL, "请输入属性名称！");
        } else {
            try {
                spAttribute.setAttrId(UUID.randomUUID().toString());
                attributeService.addAttr(spAttribute);
                log.info("添加商品属性，spAttribute={}",spAttribute);
                shopResult = new ShopResult(ShopResult.Metaa.CREATED, spAttribute);
            } catch (Exception e) {
                log.debug(e.toString());
                shopResult = new ShopResult(ShopResult.Metaa.CREATED_FAIL, e);
            }
        }
        return shopResult;
    }

    @ApiOperation(value = "修改商品属性")
    @UserLoginToken
    @PostMapping("/updAttr")
    private ShopResult updAttr(@RequestBody @ApiParam(name = "商品属性对象", value = "传入json对象", required = true) SpAttribute spAttribute) {
        ShopResult shopResult = null;
        if (!SpStringUtils.isNotEmpty(spAttribute.getCatId())) {
            shopResult = new ShopResult(ShopResult.Metaa.CREATED_FAIL, "请输入分类编号！");
        } else if (!SpStringUtils.isNotEmpty(spAttribute.getAttrSel())) {
            shopResult = new ShopResult(ShopResult.Metaa.CREATED_FAIL, "请输入属性类型！");
        } else if (!SpStringUtils.isNotEmpty(spAttribute.getAttrName())) {
            shopResult = new ShopResult(ShopResult.Metaa.CREATED_FAIL, "请输入属性名称！");
        } else if (!SpStringUtils.isNotEmpty(spAttribute.getAttrId())) {
            shopResult = new ShopResult(ShopResult.Metaa.CREATED_FAIL, "请输入属性编号！");
        } else {
            try {
                attributeService.updAttr(spAttribute);
                spAttribute=attributeService.getByParam(spAttribute).get(0);
                log.info("修改商品属性，spAttribute={}",spAttribute);
                shopResult = new ShopResult(ShopResult.Metaa.UPDATE, spAttribute);
            } catch (Exception e) {
                log.debug(e.toString());
                shopResult = new ShopResult(ShopResult.Metaa.UPDATE_FAIL, e);
            }
        }
        return shopResult;
    }

    @ApiOperation(value = "删除商品属性")
    @UserLoginToken
    @GetMapping("/delAttr")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "attrId", value = "属性编号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "catId", value = "分类编号", dataType = "String", paramType = "query")

    })
    private ShopResult delAttr(String attrId, String catId) {
        ShopResult shopResult = null;
        if (!SpStringUtils.isNotEmpty(attrId)) {
            shopResult = new ShopResult(ShopResult.Metaa.DELETED_FAIL, "请输入属性编号！");
        } else if (!SpStringUtils.isNotEmpty(catId)) {
            shopResult = new ShopResult(ShopResult.Metaa.DELETED_FAIL, "请输入分类编号！");
        } else {
            try {
                attributeService.delAttr(attrId, catId);
                log.info("删除商品属性，attrId={}",attrId);
                shopResult = new ShopResult(ShopResult.Metaa.DELETED);
            } catch (Exception e) {
                log.debug(e.toString());
                shopResult = new ShopResult(ShopResult.Metaa.DELETED_FAIL, e);
            }
        }
        return shopResult;
    }

}
